package com.itxx.keyCase.Demo3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//UDP广播
public class UdpClient {
    public static void main(String[] args) throws IOException {
        //创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        //将要传输的数据封装为byte数组
        String s = "这是通过UDP协议传输的数据";
        byte[] bytes = s.getBytes();
        int length = bytes.length;
        //将要传输的数据打包为 DatagramPacket
        DatagramPacket dp = new DatagramPacket(bytes, length, InetAddress.getByName("255.255.255.255"),54321);
        //通过DatagramSocket传递
        ds.send(dp);
        //释放资源
        ds.close();



    }
}
